<?php




/**
* emails Class
*/

class emails
extends entity
{
	/**
	* Optional Constructor: Load on demand only.
	*/
	public function __construct()
	{
		# Parent's default constructor is necessary.
		parent::__construct();

		/**
		* Set Private, Protected or Public Members
		*/
		$this->protection_code = 'e31c237fe88f253b4566312b30ba1f9b'; # Some random text, valid for the entire life
		$this->table_name = 'query_emails'; # Name of this table/entity name
		$this->pk_column = 'email_id'; # Primary Key's Column Name

		/**
		* Validation fields as used in add/edit forms
		*/
		$this->fields = array(
			# Remove the columns that you do not want to use in the ADD form
			'add' => array(
				'email_code' => null,
				'email_language' => null,
				'email_subject' => null,
				'email_comments' => null,
				'email_html' => null,
				'email_text' => null,
			),
			
			# Remove the columns that you do not want to use in the EDIT form
			'edit' => array(
				'email_code' => null,
				'email_language' => null,
				'email_subject' => null,
				'email_comments' => null,
				'email_html' => null,
				'email_text' => null,
			),
		);
	}

	/**
	* List entries from [ emails ]
	* Column `code` signifies a protection code while deleting/editing a record
	* @param $conditions SQL Conditions
	* @return Multi-Dimensional array of entries in the list
	*/
	public function list_entries(condition $condition, $from_index=0, $per_page=50)
	{
		$crud = new crud();

		/**
		* Conditions are Compiled here so that we can manupulate them individually.
		* And make them fit for [ emails ] only.
		*/
		$conditions_compiled_AND = $crud->compile_conditions(
			$condition->get_condition('AND'),
			false, 'AND', 1
		);
		$conditions_compiled_OR = $crud->compile_conditions(
			$condition->get_condition('OR'),
			false, 'OR', 2
		);

		$from_index = (int)$from_index;
		$per_page   = (int)$per_page;
		$variable   = new variable(); # It may be necessary to read list out data of a user

		$listing_sql="
SELECT SQL_CALC_FOUND_ROWS
	e.`{$this->pk_column}`, # Do not remove this
	
	# Modify these columns to your own required list (e.*)
	e.`email_code`,
	e.`email_language`,
	e.`email_subject`,
	e.`email_comments`,
	
	# Flags, load them as per your need
	e.`is_approved`,
	
	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `query_emails` `e`
WHERE
	(
		{$conditions_compiled_AND}
	)
	AND (
		{$conditions_compiled_OR}
	)
ORDER BY
	# We assume that the sorting fields are available
	e.`sink_weight` ASC,
	e.`{$this->pk_column}` DESC
LIMIT {$from_index}, {$per_page}
;";
		$this->query($listing_sql);
		$entries = $this->to_array();

		# Pagination helper: Set the number of entries
		$counter_sql="SELECT FOUND_ROWS() total;"; # Uses SQL_CALC_FOUND_ROWS from above query. So, run it immediately.
		$totals = $this->row($counter_sql);
		$this->total_entries_for_pagination = isset($totals['total'])?$totals['total']:0;

		return $entries;
	}
	
	/**
	* Details of an entity in [ emails ] for management activities only.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function details($email_id=0)
	{
		global $subdomain_id;
		
		$email_id = (int)$email_id;
		$details_sql="
SELECT
	e.`{$this->pk_column}`, # Do not remove this

	e.*, # Modify these columns,
	
	# Admin must have it to EDIT the records
	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `query_emails` `e`
WHERE
	e.`{$this->pk_column}` = {$email_id}
	AND e.is_active='Y'
	# AND e.subdomain_id={$subdomain_id}
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Details of an entity in [ emails ] for public display.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function get_details($email_id=0, $protection_code='')
	{
		$email_id = (int)$email_id;
		$protection_code = $this->sanitize($protection_code);
		$details_sql="
SELECT
	e.`{$this->pk_column}`, # Do not remove this

	e.*, # Modify these columns

	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `query_emails` `e`
WHERE
	e.`{$this->pk_column}` = {$email_id}
	AND e.is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) = '{$protection_code}'
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Flag a field; dummy use; unless you use it.
	* Every method should sanitize the user input.
	* It will co-exist with the live features.
	*/
	public function flag_field($email_id=0, $protection_code='', $field_name='')
	{
		# Allow only selected fields to be flaged Y/N
		if(!in_array($field_name, array('is_approved', 'is_featured')))
		{
			# Such flag does not exist.
			return false;
		}
		
		$email_id = (int)$email_id;
		$protection_code = $this->sanitize($protection_code);
		global $subdomain_id;

		$flag_sql="
UPDATE `query_emails` SET
	# Set your flag name here
	`{$field_name}`=IF(`{$field_name}`='Y', 'N', 'Y')
WHERE
	`{$this->pk_column}` = {$email_id}
	AND subdomain_id={$subdomain_id}
	
	# Don't touch the deleted flags
	AND is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`{$this->pk_column}`, '{$this->protection_code}')) = '{$protection_code}'
;";
		return $this->query($flag_sql);
		#return false; # dummy mode only
	}

	/**
	* Welcome and ask for authentication?
	* Please extend this method according to your business logic.
	* eg. Send email to the first signed up member, trigger something else when a data is added.
	* Called right after a new [ emails ] is added: insert-hook.
	*/
	public function welcome_first($email_id=0)
	{
		$email_id = (int)$email_id;
	 	
		return true;
	}
}
?>